package com.example.administrator.ygddriver.base;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.example.administrator.ygddriver.tool.MyApp;
import com.example.administrator.ygddriver.tool.UrlValuse;
import com.example.administrator.ygddriver.welcome.WelcomeActivity;
import com.google.gson.Gson;

import cn.jpush.android.api.JPushInterface;

public class TalkReceiver extends BroadcastReceiver {
    private static final String TAG = "TalkReceiver";

    private NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }

        Bundle bundle = intent.getExtras();

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Log.d(TAG, "JPush 用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
//            Log.d(TAG, "接受到推送下来的通知");
//            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
//            Log.d("TalkReceiver", " title : " + title);
//            String message = bundle.getString(JPushInterface.EXTRA_ALERT);
//            Log.d("TalkReceiver", "message : " + message);
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            Gson gson = new Gson();
            JGRequestBean jgRequestBean = gson.fromJson(extras, JGRequestBean.class);
            Log.d("TalkReceiver", "extras : " + extras);
            Intent intent1 = new Intent();
            Bundle bundle1 = new Bundle();
            bundle1.putParcelable("jgRequestBean", jgRequestBean);
            intent1.putExtras(bundle1);
            intent1.setAction(UrlValuse.JG);
            MyApp.context.sendBroadcast(intent1);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "用户点击打开了通知");
            Intent intent1 = new Intent(MyApp.context, WelcomeActivity.class);
            MyApp.context.startActivity(intent1);
        } else {
            Log.d(TAG, "Unhandled intent - " + intent.getAction());
        }
    }
}
